﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

namespace SendEmail
{
    class SendEmail
    {
        SmtpClient SmtpClient = null;   //设置SMTP协议
        MailAddress MailAddress_from = null; //设置发信人地址  当然还需要密码
        MailAddress MailAddress_to = null;  //设置收信人地址  不需要密码
        MailMessage MailMessage_Mai = null;
        FileStream FileStream_my = null; //附件文件流

        #region 设置Ｓmtp服务器信息
        /// <summary>
        /// 设置Ｓmtp服务器信息
        /// </summary>
        /// <param name="ServerName">SMTP服务名</param>
        /// <param name="Port">端口号</param>
        private void setSmtpClient(string ServerHost, int Port)
        {
            SmtpClient = new SmtpClient();
            SmtpClient.Host = ServerHost;//指定SMTP服务名  例如QQ邮箱为 smtp.qq.com 新浪cn邮箱为 smtp.sina.cn等
            SmtpClient.Port = Port; //指定端口号
            SmtpClient.Timeout = 0;  //超时时间

        }
        #endregion

        #region 验证发件人信息
        /// <summary>
        /// 验证发件人信息
        /// </summary>
        /// <param name="MailAddress">发件邮箱地址</param>
        /// <param name="MailPwd">邮箱密码</param>
        private void setAddressform(string MailAddress, string MailPwd)
        {
            //创建服务器认证
            NetworkCredential NetworkCredential_my = new NetworkCredential(MailAddress, MailPwd);
            //实例化发件人地址
            MailAddress_from = new System.Net.Mail.MailAddress(MailAddress);
            //指定发件人信息  包括邮箱地址和邮箱密码
            
            
            SmtpClient.Credentials = new System.Net.NetworkCredential(MailAddress_from.Address, MailPwd);
            //SmtpClient.UseDefaultCredentials = true;
        }
        #endregion
        #region 检测附件大小
        private bool Attachment_MaiInit(string path)
        {

            try
            {
                FileStream_my = new FileStream(path, FileMode.Open);
                string name = FileStream_my.Name;
                int size = (int)(FileStream_my.Length / 1024 / 1024);
                FileStream_my.Close();
                //控制文件大小不大于10Ｍ
                if (size > 10)
                {

                    return false;
                }

                return true;
            }
            catch (IOException E)
            {
                return false;
            }

        }
        #endregion
        public void Send(string toAddressList, string smtpServer, string fromAddress, string fromPassword, string subject, string message)
        {
            //检测附件大小 发件必需小于10M 否则返回  不会执行以下代码
            //if (toAddress != "")
            //{
            //    if (!Attachment_MaiInit(toAddress.Trim()))
            //    {
            //        return;
            //    }
            //}
            if (smtpServer == "")
            {
                return;
            }
            if (fromAddress == "")
            {
                return;
            }
            if (fromPassword == "")
            {
                return;
            }

            try
            {
                //初始化Ｓmtp服务器信息
                setSmtpClient(smtpServer, 26);
            }
            catch (Exception Ex)
            {
                return;
            }
            try
            {
                //验证发件邮箱地址和密码
                setAddressform(fromAddress.Trim(), fromPassword.Trim());
            }
            catch (Exception Ex)
            {
                return;
            }
            MailMessage_Mai = new MailMessage();
            //发件人邮箱
            MailMessage_Mai.From = MailAddress_from;
            //邮件主题
            MailMessage_Mai.Subject = subject;
            MailMessage_Mai.SubjectEncoding = System.Text.Encoding.UTF8;
            //邮件正文
            MailMessage_Mai.Body = message;
            MailMessage_Mai.BodyEncoding = System.Text.Encoding.UTF8;
            //清空历史附件  以防附件重复发送
            MailMessage_Mai.Attachments.Clear();
            //添加附件
            //MailMessage_Mai.Attachments.Add(new Attachment(toAddress.Trim(), MediaTypeNames.Application.Octet));
            //注册邮件发送完毕后的处理事件
            foreach (var toAddress in toAddressList.Split(','))
            {
                if (toAddress.Contains("@"))
                    MailMessage_Mai.To.Add(toAddress.Trim());
            }
            //SmtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
            //开始发送邮件
            //SmtpClient.SendAsync(MailMessage_Mai, "000000000");
            try
            {
                SmtpClient.Timeout = 60000;
                SmtpClient.Send(MailMessage_Mai);
            }
            catch (Exception)
            {
                    
                throw;
            }
        }

    }
}
